package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/9 9:41
 * https://leetcode-cn.com/problems/happy-number/
 */
public class Title202 {

    /**
     *
     * @param n
     * @return
     */
    public static boolean isHappy(int n) {
        int slow = n;
        int fast = getNext(n);
        while (slow != fast) {
            slow = getNext(slow);
            fast = getNext(getNext(fast));
        }
        return slow == 1;
    }

    public static int getNext(int n) {
        int sum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            sum += d * d;
        }
        return sum;
    }






    public boolean isHappy1(int n) {
        int slow = n;
        int fast = getNum(n);
        while (slow != fast) {
            slow = getNum(slow);
            fast = getNum(getNum(fast));
        }
        return slow == 1;
    }

    public int getNum(int n) {
        int sum = 0;
        while (n != 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }







    public static void main(String[] args) {
        isHappy(19);

    }
}
